package com.jeasonzhao.commons.logger;

public class BasicLogger implements Logger
{
    private java.util.ArrayList<LogListener> m_listener = new java.util.ArrayList<LogListener>();
    public BasicLogger(LogListener ...logListener)
    {
        super();
        if(null != logListener)
        {
            for(LogListener ls : logListener)
            {
                if(null != ls)
                {
                    m_listener.add(ls);
                }
            }
        }
    }

    public void addListener(LogListener ...logListener)
    {
        if(null != logListener)
        {
            for(LogListener ls : logListener)
            {
                if(null != ls)
                {
                    m_listener.add(ls);
                }
            }
        }
    }

    public void addListener(BasicLogger logger)
    {
        if(null != logger && null != logger.m_listener)
        {
            for(LogListener ls : logger.m_listener)
            {
                if(null != ls)
                {
                    m_listener.add(ls);
                }
            }
        }
    }

    /**
     *@TODO asx
     */
    protected void log(LogLevel level,String str,Throwable excep)
    {
        if(m_listener == null)
        {
            return;
        }
        else
        {
            for(LogListener ls : m_listener)
            {
                if(null != ls)
                {
                    try
                    {
                        ls.log(level,str,excep);
                    }
                    catch(Throwable ex)
                    {
                    	ex.printStackTrace();
                    }
                }
            }
        }
    }

    public void error(String str,Throwable excep)
    {
        log(LogLevel.ERROR,str,excep);
    }

    public void system(String str,Throwable excep)
    {
        log(LogLevel.SYSTEM,str,excep);
    }

    public void fatal(String str,Throwable excep)
    {
        log(LogLevel.FATAL,str,excep);
    }

    public void warn(String str,Throwable excep)
    {
        log(LogLevel.WARN,str,excep);
    }

    public void info(String str,Throwable excep)
    {
        log(LogLevel.INFO,str,excep);
    }

    public void debug(String str,Throwable excep)
    {
        log(LogLevel.DEBUG,str,excep);
    }

    public void error(String str)
    {
        log(LogLevel.ERROR,str,null);
    }

    public void system(String str)
    {
        log(LogLevel.SYSTEM,str,null);
    }

    public void fatal(String str)
    {
        log(LogLevel.FATAL,str,null);
    }

    public void warn(String str)
    {
        log(LogLevel.WARN,str,null);
    }

    public void info(String str)
    {
        log(LogLevel.INFO,str,null);
    }

    public void debug(String str)
    {
        log(LogLevel.DEBUG,str,null);
    }

    public static final BasicLogger DUMMY = new BasicLogger();
}
